package experiment5;

import java.util.*;

public class DefiniteIntegral {
    Random randomNumber;
    DefiniteIntegral(Random random){
        randomNumber = random;
    }

    public double solve(Double a, Double b, int n, Function fun) {
        double y = 0d;
        double c;
        for (int i = 0; i <= n; i++) {
            double x = (b - a) * randomNumber.nextDouble() + a;
            y += fun.function(x);
        }
        return (b - a) * y / (double) (n);
    }

    public static void main(String[] args) {
        DefiniteIntegral definiteIntegral = new DefiniteIntegral(new RandomNumber());
        DefiniteIntegral definiteIntegral1 = new DefiniteIntegral(new Random());
        Scanner scanner = new Scanner(System.in);
        Double a = Double.parseDouble(scanner.next());
        Double b = Double.parseDouble(scanner.next());
        int n = scanner.nextInt();

        HashMap<String, Function> functionList = new HashMap<>();
        functionList.put("x * x", x -> x * x);
        functionList.put("x * x * x", x -> x * x * x);
        functionList.put("sqrt(x)", x -> Math.sqrt(x));
        functionList.put(" x -> 1 / 3 * x * x * x + 1 / 10 * x + 5", x -> 1 / 3 * x * x * x + 1 / 10 * x + 5);

        for (Map.Entry<String, Function> entry : functionList.entrySet()) {
            System.out.println(entry.getKey() + ":");
            System.out.println("RandomNumber:" + definiteIntegral.solve(a, b, n, entry.getValue()));
            System.out.println("Random:" + definiteIntegral1.solve(a, b, n, entry.getValue()));
        }

    }


}
